package org.example.eight3.dao.impl;

import org.example.eight3.dao.UserDao;
import org.example.eight3.model.User;
import org.example.eight3.util.DBUtil;

import java.sql.*;

public class UserDaoImpl implements UserDao {
    public User findByUserName(String name) {
        String sql= "select * from user where username = ?";
        User user = new User();
        try(Connection conn = DBUtil.getConn();
            PreparedStatement prep = conn.prepareStatement(sql);
        ){
            prep.setString(1,name);
            try(ResultSet rs = prep.executeQuery()){
                while (rs.next()) {
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setEmail(rs.getString("email"));
                    user.setPhone(rs.getString("phone"));
                    user.setName(rs.getString("name"));
                    user.setRole(rs.getString("role"));
                    user.setStatus(rs.getInt("status"));
                    user.setCreate_time(rs.getTimestamp("create_time").toLocalDateTime());
                }
                return user;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

